<template>
  <div class="registerDetail">
    <van-nav-bar
      :title="$route.meta.title"
      left-text="返回"
      left-arrow
      :fixed="true"
      :placeholder="true"
      @click-left="$router.go(-1)"
    />
    <div class="top_fixCenter">
      <i class="iconfont icon-tips"></i>当前组织为<span class="txt">{{
        formData.fixCenterName
      }}</span
      >，对应采购展厅为<span class="txt">{{
        showOrgList && showOrgList[0] ? showOrgList[0].name : "-"
      }}</span
      >，若不是请自行切换推荐组织
    </div>
    <!-- 表单 -->
    <div class="formData">
      <van-form ref="form">
        <van-field
          v-model="formData.fixCenterName"
          readonly
          required
          name="推荐人所属组织"
          label="推荐人所属组织"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
          @click="handleSelect('fixCenterId', 'fixCenterName')"
          :rules="[{ required: true }]"
          label-width="140px"
        />
        <van-field
          required
          v-model="formData.leadsSourceName"
          name="线索来源"
          label="线索来源"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
          @click="handleSelect('leadsSource', 'leadsSourceName')"
          :rules="[{ required: true }]"
          readonly
        />
        <van-field
          required
          v-model="formData.customerName"
          name="客户名称"
          label="客户名称"
          input-align="right"
          placeholder="请输入"
          :rules="[{ required: true }]"
        />
        <van-field
          required
          v-model="formData.phone"
          name="手机号码"
          label="手机号码"
          input-align="right"
          type="tel"
          placeholder="请输入"
          :rules="[
            { required: true },
            { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式错误！' },
          ]"
        />
        <van-field
          v-model="formData.carModelName"
          name="车型名称"
          label="车型名称"
          input-align="right"
          placeholder="请选择"
        />
        <van-field
          v-model="formData.carNo"
          name="车牌号码"
          label="车牌号码"
          input-align="right"
          placeholder="请输入"
        />
        <van-field
          required
          name="radio"
          label="车辆是否在店"
          input-align="right"
        >
          <template #input>
            <van-radio-group
              v-model="formData.isCarInShop"
              direction="horizontal"
            >
              <van-radio
                :name="item.value"
                v-for="(item, index) in isflagList"
                :key="index"
                >{{ item.name }}</van-radio
              >
            </van-radio-group>
          </template>
        </van-field>
        <van-field
          required
          v-model="formData.userName"
          name="线索提供人"
          label="线索提供人"
          input-align="right"
          placeholder="请输入"
          readonly
        />
        <van-field
          required
          v-model="formData.note"
          name="备注"
          label="备注"
          type="textarea"
          input-align="left"
          placeholder
        />

        <!-- 下拉选择弹窗 -->
        <van-popup v-model="showSelectPicker" position="bottom">
          <van-picker
            show-toolbar
            :columns="columns"
            @confirm="handleSelectConfirm"
            @cancel="showSelectPicker = false"
            value-key="name"
          />
        </van-popup>
        <div class="footerBtn">
          <div class="submitBtn" @click="formOnSubmit(2)">提交</div>
          <div class="saveBtn" @click="formOnSubmit(1)">保存</div>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import {
  Search,
  Empty,
  Field,
  Form,
  Uploader,
  DatetimePicker,
  Tab,
  Tabs,
  ImagePreview,
  Notify,
} from "vant";
import taskApi from "@/api/taskApi";
import { getCookies } from "@/utils/util.js";
import commonApi from "@/api/commonApi";
import sellManageApi from "@/api/sellManageApi";
export default {
  name: "taskCenterEdit",
  components: {
    Search,
    Empty,
    Field,
    Form,
    Uploader,
    DatetimePicker,
    Tab,
    Tabs,
    ImagePreview,
    Notify,
  },
  data() {
    return {
      showTimerPicker: false,
      dataSource: {
        billId: "1", //单据id
        taskTypeName: "定期保养", //任务类型
        taskName: "闽D66666定期保养", //任务名称
        oneBillStatus: "未处理", //任务一级状态
        twoBillStatus: "未处理", //任务二级状态
        carModelName: "Ct5 2.0T", //车型
        customerName: "张三", //客户名称
        mobile: "187231231", //电话
        ownerName: "李四", //车主姓名
        ownerPhone: "187231231", //车主电话
      },
      formData: {
        saveOrSubmit: "", //固定值,save是保存动作,submit是提交
        leadsSource: "", //线索来源，String，提交必填
        leadsSourceName: "", //线索来源，String，提交必填
        customerName: "", //客户名称,提交必填，String
        phone: "", //手机号码,提交必填，String
        carModelName: "", //车型名称值,提交必填，String
        carNo: "", //车牌号码,提交必填，String
        isCarInShop: 1, //车辆是否到店,提交必填
        orgId: "", //组织ID,必填
        userId: getCookies("userId"), //用户ID，必填，String
        userName: "",
        note: "",
        fixCenterId: "",
        fixCenterName: "",
      },
      columns: [],
      showSelectPicker: false,
      detailId: "",
      userId: getCookies("userId"), //用户ID，必填，String
      isflagList: [
        { name: "是", value: 1 },
        { name: "否", value: 0 },
      ],
      leadsSourceList: [],
      field: "",
      fieldName: "",
      fixCenterList: [],
      showOrgList: [],
    };
  },

  mounted() {
    if (this.$route.query && this.$route.query.billId) {
      this.detailId = this.$route.query.billId;
      this.getDetail(this.detailId);
    }
    this.getLeadsSource();
    this.getMainFixCenter();
    if (getCookies("userInfo")) {
      this.formData.userName = JSON.parse(getCookies("userInfo")).kdusername;
    }
  },
  methods: {
    // 获取维修中心
    getMainFixCenter() {
      commonApi
        .getMainFixCenter({
          orgId: "test",
          userId: this.userId,
          type: "",
        })
        .then((res) => {
          this.fixCenterList = res.data;
          if (!this.$route.query.billId) {
            this.fixCenterList.forEach((item) => {
              if (localStorage.getItem("orgId") == item.billId) {
                this.formData.fixCenterId = item.billId;
                this.formData.fixCenterName = item.name;
                this.getShowOrgByFixCenterId();
              }
            });
          }
        });
    },
    // 获取展厅
    getShowOrgByFixCenterId() {
      commonApi
        .getShowOrgByFixCenterId({
          fixCenterId: this.formData.fixCenterId,
          userId: this.userId,
        })
        .then((res) => {
          if (res.data) {
            this.showOrgList = res.data;
          }
        });
    },
    getDetail(billId) {
      sellManageApi
        .secondHandCarFacade({
          method: "getPurchaseLeadsById", //固定值
          billId, //单据Id，必填，String
        })
        .then((res) => {
          if (res.code == 1) {
            this.formData = res.data;
            this.formData.isCarInShop =
              this.formData.carIsInShop == "是" ? 1 : 0;
            this.formData.userName = this.formData.providePerson;
            this.leadsSourceList.forEach((item) => {
              if (this.formData.leadsSource == item.name) {
                this.formData.leadsSourceName = item.name;
                this.formData.leadsSource = item.value;
              }
            });
          }
        });
    },

    // 获取线索来源
    getLeadsSource() {
      commonApi
        .getEnums({
          key: "leadsSource",
          userId: this.userId,
        })
        .then((res) => {
          if (res.data) {
            this.leadsSourceList = res.data;
          }
        });
    },

    // 选择下拉
    handleSelect(field, fieldName) {
      this.showSelectPicker = true;
      if (field == "leadsSource") {
        this.columns = this.leadsSourceList;
      } else if (field == "fixCenterId") {
        this.columns = this.fixCenterList;
      }
      this.field = field;
      this.fieldName = fieldName;
    },
    // 选择下拉确认
    handleSelectConfirm(value, index) {
      if (this.field == "fixCenterId") {
        this.formData[this.field] = value.billId;
        this.formData[this.fieldName] = value.name;
        this.getShowOrgByFixCenterId();
      } else {
        this.formData[this.field] = value.value;
        this.formData[this.fieldName] = value.name;
      }
      this.showSelectPicker = false;
    },
    formOnSubmit(type) {
      this.$refs.form
        .validate()
        .then(() => {
          sellManageApi
            .secondHandCarFacade({
              ...this.formData,
              method: this.formData.billId
                ? "editPurchaseLeads"
                : "addPurchaseLeads",
              saveOrSubmit: type == 1 ? "save" : "submit",
            })
            .then((res) => {
              console.log(res);
              if (res.code == 1) {
                if (type == 1) {
                  Notify({ type: "success", message: "保存成功" });
                  this.formData.billId = res.billId;
                } else {
                  Notify({ type: "success", message: "提交成功" });
                  this.formData.billId = res.billId;
                  this.$router.go(-1);
                }
              }
            });
          this.loading = true;
        })
        .catch(() => {
          this.loading = false;
          this.$toast.fail("请完善信息");
        });
    },
  },
};
</script>

<style lang="less" scoped>
body {
  background: #f3f3f3;
}
.flex {
  display: flex;
  align-items: center;
}
.registerDetail {
  overflow: hidden;
  .detail_header {
    justify-content: space-between;
    margin: 0.2rem 0.2rem;
    border-radius: 5px;
    background: #fff;
    padding: 0.3rem;
    .detail_header_lt {
      .carNo {
        font-size: 0.32rem;
        font-weight: bold;
        margin-bottom: 0.2rem;
        .carStatus {
          border: 1px solid #35bc04;
          color: #35bc04;
          border-radius: 5px;
          padding: 0.1rem 0.1rem;
          font-size: 0.24rem;
          text-align: center;
          margin-left: 0.1rem;
        }
      }
      .hank {
        font-size: 0.28rem;
        color: #666;
        .name {
          border-right: 2px solid #ccc;
          padding-right: 0.1rem;
          margin-right: 0.1rem;
        }
      }
    }
    .detail_header_rt {
      justify-content: flex-start;
      .txt {
        font-size: 0.28rem;
        color: #161616;
      }
      .icon-tel {
        color: #fff;
        background: #35bc04;
        font-size: 0.32rem;
        padding: 0.1rem;
        border-radius: 50%;
        margin-right: 0.1rem;
      }
    }
  }
  .formData {
    background: #fff;
    overflow: hidden;
  }
}

.footerBtn {
  // display: flex;
  // justify-content: space-between;
  width: 80%;
  margin: auto;
  padding-bottom: 1rem;
  background: #fff;
  margin-top: 0.5rem;
  .saveBtn {
    background: #ccc;
    color: #fff;
    margin: auto;
    text-align: center;
    padding: 0.25rem;
    border-radius: 5px;
    font-size: 0.3rem;
  }
  .submitBtn {
    background: #409eff;
    color: #fff;
    margin: auto;
    text-align: center;
    padding: 0.25rem;
    border-radius: 5px;
    font-size: 0.3rem;
    margin-bottom: 0.2rem;
  }
}
.top_fixCenter {
  background: #ffad43;
  color: #fff;
  padding: 0.2rem;
  font-size: 0.28rem;
  line-height: 16px;
  .txt {
    color: #ff2c1d;
    font-weight: bold;
  }
  .icon-tips {
    font-size: 14px;
    margin-right: 0.1rem;
  }
}
</style>
